#usage:
#Archive adhoc package:     ./compile_ios_release.sh "runtime iOS" IslandMatch_Provisioning_Adhoc
#Archive appstore package:  ./compile_ios_release.sh "runtime iOS" IslandMatch_Provisioning_Distribution

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd )
PROJECT_PATH="$DIR/../.."

pushd ${DIR}/../../proj.ios

SCHEME="$1"
PROVISIONING_PROFILE="$2"
SIGN_METHOD="$3"
PROJECT_NAME="$4"
APP_NAME="$5"
IOS_APP_NAME="$6"

CERT_PATH="../../../../Documents/Cert/${PROJECT_NAME}"
CONFIG_PATH="${DIR}/config"

if [ -z "$SCHEME" ]
then
	echo "Scheme unset, default as runtime iOS"
	SCHEME="Unity-iPhone"
else
	echo "Scheme is: $SCHEME"
fi

function updateProvision {
	pushd ~/Library/MobileDevice/Provisioning\ Profiles
	grep -l "\b$1\b" * | xargs /bin/rm -rf
	popd

	pushd ~/Documents/Cert/${PROJECT_NAME}
	MP_FILE_PATH="${PROJECT_NAME}_$1.mobileprovision"
	open $MP_FILE_PATH
	echo "mobileprovision file path: $MP_FILE_PATH"

	PROVISIONING_UUID=`${DIR}/mpParse -f "$MP_FILE_PATH" -o UUID`
	echo "Provisioning profile is: $PROVISIONING_UUID"
	popd
}

if [ -z "$PROVISIONING_PROFILE" ]
then
	echo "Provisioning profile unset, need privisioning profile filename"
	exit 1
else
	updateProvision ${PROVISIONING_PROFILE}
fi

ARCHIVE_NAME="ReleaseArchive"

if [[ -d $ARCHIVE_NAME.xcarchive ]]
then
	echo "Clear existing $ARCHIVE_NAME.xcarchive"
	rm -rf $ARCHIVE_NAME.xcarchive
else
	echo "No $ARCHIVE_NAME.xcarchive exist"
fi

if [[ -d $ARCHIVE_NAME ]]
then
	echo "Clear existing $ARCHIVE_NAME"
	rm -rf $ARCHIVE_NAME
else
	echo "No $ARCHIVE_NAME exist"
fi
# Fix issue of 'User interaction is not allowed'
#security unlock-keychain -p $JENKINS_PASSWORD /Users/Shared/Jenkins/Library/Keychains/login.keychain

xcodebuild clean -scheme "$SCHEME" -configuration Release

if [ -e 'Podfile' ]
then
	#echo "Build with Podfile"
	# export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;
	#pod install

	#xcodebuild archive  -workspace 项目名称.xcworkspace  -scheme 项目名称  -configuration 构建配置  -archivePath archive包存储路径 CODE_SIGN_IDENTITY=证书  PROVISIONING_PROFILE=描述文件UUID
	# First build the archive
	xcodebuild archive -workspace 'Unity-iPhone.xcworkspace' -scheme "$SCHEME" -destination generic/platform=iOS \
			PROVISIONING_PROFILE="$PROVISIONING_UUID" -archivePath "$ARCHIVE_NAME" && \
	echo "Xcode Archive done"
else
	echo "Build without Podfile"
	# First build the archive
	xcodebuild archive -scheme "$SCHEME" -destination generic/platform=iOS \
			PROVISIONING_PROFILE="$PROVISIONING_UUID" -archivePath "$ARCHIVE_NAME" && \

	echo "xcode archive finished"
fi

EXPORT_PLIST="exportOptionsAdhoc.plist"
xcodebuild -exportArchive -archivePath "$ARCHIVE_NAME".xcarchive \
	-exportPath "$ARCHIVE_NAME" -exportOptionsPlist ${CONFIG_PATH}/${EXPORT_PLIST}
mv ${PROJECT_PATH}/proj.ios/ReleaseArchive/${IOS_APP_NAME}.ipa ${PROJECT_PATH}/proj.ios/ReleaseArchive/${APP_NAME}_Adhoc.ipa

if [ "$SIGN_METHOD" == "AppStore" ]
then
	EXPORT_PLIST="exportOptionsAppstore.plist"
	xcodebuild -exportArchive -archivePath "$ARCHIVE_NAME".xcarchive \
		-exportPath "$ARCHIVE_NAME" -exportOptionsPlist ${CONFIG_PATH}/${EXPORT_PLIST}
	mv ${PROJECT_PATH}/proj.ios/ReleaseArchive/${IOS_APP_NAME}.ipa ${PROJECT_PATH}/proj.ios/ReleaseArchive/${APP_NAME}_AppStore.ipa
fi

pushd ${DIR}/../../proj.ios/${ARCHIVE_NAME}.xcarchive
zip -r dSYMs.zip dSYMs
popd

popd